﻿using FluentAssertions;
using Microsoft.Extensions.Logging;
using RedLockNet.SERedis;
using RedLockNet.SERedis.Configuration;
using System;
using System.Collections.Generic;

namespace NewRetail.Application.Core
{
    public class RedLockFactoryWrapper : IDisposable
    {
        public RedLockFactory RedLockFactory { get; }

        public RedLockFactoryWrapper(RedisClient wrapper, ILoggerFactory loggerFactory)
        {
            wrapper.Should().NotBeNull();
            loggerFactory.Should().NotBeNull();
            var multiplexers = new List<RedLockMultiplexer>
            {
                new RedLockMultiplexer(wrapper.Connection)
            };

            foreach (var m in multiplexers)
            {
                m.RedisDatabase = 5;
            }

            this.RedLockFactory = RedLockFactory.Create(multiplexers, loggerFactory);
        }

        public void Dispose()
        {
            this.RedLockFactory?.Dispose();
        }
    }
}